package view;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import controller.Cashier;

import model.AccountException;

/**
 * Servlet implementation class CommitmentServlet
 */
public class CommitmentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Cashier cashier = null;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CommitmentServlet() {
        super();
        cashier = Cashier.getInstance();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String forward = "/Controller.jsp";
		String removeCommitmentStatus = "";
		String[] commintmentIDs = request.getParameterValues("removerCompromisso");

		String[] allCommintments = request.getParameterValues("removerTodos");
		
		if(allCommintments != null){
			if(allCommintments.length > 0){
				cashier.removeAllCommitments();
				removeCommitmentStatus = "Todos os compromissos removidos...";
			}
		}
		
		if((commintmentIDs != null) & !removeCommitmentStatus.equals("Todos os compromissos removidos...")){
			for (int i = 0; i < commintmentIDs.length; i++) {
				try {
					cashier.removeCommitment(commintmentIDs[i]);
				} catch (AccountException e) {
					removeCommitmentStatus = e.getMessage();
				}
			}
		}
		
		try {
			cashier.generateReport(null, null, null, (String) request.getSession().getAttribute("exibirConta"));
		} catch (AccountException e) {
			removeCommitmentStatus = e.getMessage();
		}
		
		request.setAttribute("removeCommitmentStatus", removeCommitmentStatus);
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(forward);
		if (dispatcher != null) {
			dispatcher.forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String forward = "/Controller.jsp";
		String addCommitmentStatus = "";
		
		try {
			cashier.addCommitment(request.getParameter("label"), request.getParameter("date"), new BigDecimal(request.getParameter("value")), request.getParameter("type"), request.getParameter("frequecy"), request.getParameter("description"));//((String)request.getSession().getAttribute("exibirConta"), request.getParameter("type"), request.getParameter("label"), new BigDecimal(request.getParameter("value")), request.getParameter("description"), request.getParameter("date"));
			cashier.generateReport(null, null, null, (String)request.getSession().getAttribute("exibirConta"));
			addCommitmentStatus = "Compromisso adicionado com sucesso!";
		} catch (AccountException e) {
			addCommitmentStatus = e.getMessage();
		} catch (ParseException e) {
			
			addCommitmentStatus = e.getMessage();
		}
		
		request.setAttribute("addCommitmentStatus", addCommitmentStatus);
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(forward);
		if (dispatcher != null) {
			dispatcher.forward(request, response);
		}
	}

}
